package com.fate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fate.domain.Basket;
import com.fate.service.BasketService;
import com.fate.vo.CartMoney;
import com.fate.vo.ShopCartResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("p/shopCart")
@Api(tags = "购物车管理接口")
public class BasketController {

    @Autowired
    private BasketService basketService;

    @GetMapping("prodCount")
    @ApiOperation("查询用户购物车总条数")
    public ResponseEntity<Integer> getBasketCount() {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Integer count = basketService.getBasketCountByUserId(userId);
        return ResponseEntity.ok(count);
    }

    /**
     * 商品里面修改 可能是增加 也可能是修改
     * skuid
     * prodid
     * count
     *
     * @return
     */
    @PostMapping("changeItem")
    @ApiOperation("改变购物车商品信息")
    public ResponseEntity<Void> changeItem(@RequestBody Basket basket) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        basket.setUserId(userId);
        basketService.changeItem(basket);
        return ResponseEntity.ok().build();
    }

    /**
     * 因为某些商品可能公用一些优惠券
     * <p>
     * 如果再包装一层 最外面通用的优惠券可以适配所有业务场景
     *
     * @return
     */
    @GetMapping("info")
    @ApiOperation("查询购物车详情")
    public ResponseEntity<List<ShopCartResult>> cartInfo() {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        List<ShopCartResult> shopCartResults = basketService.getCartInfo(userId);
        return ResponseEntity.ok(shopCartResults);
    }

    @DeleteMapping("deleteItem")
    @ApiOperation("删除购物车选中的商品")
    public ResponseEntity<Void> cartInfo(@RequestBody List<Long> basketIds) {
        basketService.removeByIds(basketIds);
        return ResponseEntity.ok().build();
    }


    /**
     * 返回一个对象
     * 最终金额
     * 实际金额
     * 优惠了多少钱
     * 运费
     *
     * @param basketIds
     * @return
     */
    @PostMapping("totalPay")
    @ApiOperation("计算购物车选中的商品总金额")
    public ResponseEntity<CartMoney> totalPay(@RequestBody List<Long> basketIds) {
        CartMoney cartMoney = basketService.calcCartMoney(basketIds);
        return ResponseEntity.ok(cartMoney);
    }

    /**
     * 提供远程调用购物车 根据ids拿到集合
     *
     * @param basketIds
     * @return
     */
    @PostMapping("getCartsByIds")
    @ApiOperation("提供远程调用购物车 根据ids拿到集合")
    List<Basket> getCartsByIds(@RequestBody List<Long> basketIds) {
        return (List<Basket>) basketService.listByIds(basketIds);

    }

    /**
     * 提供远程调用清空购物车的方法
     */
    @PostMapping("clearCart")
    @ApiOperation("提供远程调用清空购物车的方法")
    void clearCart(@RequestParam String userId ,@RequestBody List<Long> skuIds){
        basketService.remove(new LambdaQueryWrapper<Basket>()
            .eq(Basket::getUserId,userId)
                .eq(Basket::getSkuId,skuIds)
        );
    }

}
